<script>
// #ifdef WEB
import jssdk from "weixin-js-sdk";
// #endif
export default {
  onLaunch: function () {
    const systemInfo = uni.getSystemInfoSync();
    const system = systemInfo.system.toLowerCase();
    // safeAreaHeight (rpx)
    if (systemInfo.safeArea) {
      let safeAreaHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom;
      this.globalData.safeAreaHeight = parseInt(safeAreaHeight * 750 / systemInfo.screenWidth)
    }
    // statusBarHeight、navBarHeight (px)
    this.globalData.statusBarHeight = systemInfo.statusBarHeight;
    this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
    if (systemInfo.screenWidth > 960) {
      this.globalData.navBarHeight = systemInfo.statusBarHeight + 66;
    }

    if (system.indexOf('android') > -1) {
      this.globalData.system = 'android'
    } else if (system.indexOf('ios') > -1) {
      this.globalData.system = 'ios'
    } else {
      this.globalData.system = 'other'
    }

    // #ifdef WEB
    var ua = navigator.userAgent.toLowerCase();
    var isWechat = ua.match(/micromessenger/i) && !ua.match(/windows/i) && !ua.match(/macos/i) && !ua.match(/macwechat/i);
    if (isWechat) {
      this.$util.request({
        url: '/h5/getShareInfo',
        data: {
          url: window.location.href
        }
      }).then((res) => {
        const jssdkConfig = res.data.jssdk_config;
        const share_title = res.data.share_title;
        const share_link = res.data.share_link;
        const share_desc = res.data.share_desc;
        const share_image = res.data.share_image;
        if (jssdkConfig) {
          jssdk.config(jssdkConfig);
          jssdk.ready(function() {
            jssdk.updateAppMessageShareData({
              title: share_title,
              desc: share_desc,
              link: share_link,
              imgUrl: share_image,
              success: function() {
                // 设置成功
              }
            })
            jssdk.updateTimelineShareData({
              title: share_title,
              link: share_link,
              imgUrl: share_image,
              success: function() {
                // 设置成功
              }
            })
          });
        }
        this.globalData.jssdk = jssdk
      });
    }
    // #endif
  },
  globalData: {
    siteinfo: require('./siteinfo.js'),
    jssdk: null,
    system: '',
    safeAreaHeight: 0,
    statusBarHeight: 0,
    navBarHeight: 0,
    user_id: 0
  }
}
</script>

<style>
@import './app.css';
</style>
